﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RouterControl.LocalDb
{
    partial class LocalDbConnector
    {
        public struct OnlineStatusInfo
        {
            public DateTime TimeStamp;
            public string OldStatus;
            public string NewStatus;

            public static readonly OnlineStatusInfo Empty = new OnlineStatusInfo()
            {
                TimeStamp = DateTime.MinValue
            };
        }

        public void AddOnlineStatusInfo(DeviceInfo device, OnlineStatusInfo info)
        {
            if (device.Id <= 0)
                throw new ArgumentException("invalid device", "device");

            try
            {
                // query data
                var cmd = _db.CreateCommand();

                cmd.CommandText = "insert into OnlineStatus (DeviceId,TimeStamp,StatusOld,StatusNew) values (@id,@t,@o,@n)";
                cmd.Parameters.AddWithValue("@id", device.Id);
                cmd.Parameters.AddWithValue("@t", info.TimeStamp);
                cmd.Parameters.AddWithValue("@o", info.OldStatus);
                cmd.Parameters.AddWithValue("@n", info.NewStatus);

                // insert
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                _log.Error("Local DB failed: AddOnlineStatusInfo()", ex);

                throw;
            }
        }

    }
}
